greedy

Please click on ads to support us..

Python Code:

from collections import Counter
def main():
  t = int(input())
  for t in range(1, t + 1):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    dmin = [0] * n
    dmax = [0] * n
    j = 0
    for i in range(n):
      while b[j] < a[i]:
        j += 1
      dmin[i] = b[j] - a[i]

    j = 0
    for i in range(n):
      j = max(i, j)
      while j+1 < n and a[j+1] <= b[j]:
        j += 1
      dmax[i] = b[j] - a[i]

    print(" ".join(list(map(str, dmin))))
    print(" ".join(list(map(str, dmax))))



if __name__ == "__main__":
  main()

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
 
bool visited[100009];
bool on_stack[100009];
unordered_map<int,vector<int>> d;
vector<int> ans;
 

// Fiding the d_min: Find the first thing larger than or equal to the current index - maybe two pointer?
// Finding the d_largest: The issue is that you can't always go for the max, because you could have a greater number i
 
int main() {
    int t;
    cin>>t;
    for(int i = 0; i<t; i++){
        int n;
        cin>>n;
        vector<int> a;
        vector<int> b;
        for(int j = 0; j<n; j++){
            int g;
            cin>>g;
            a.push_back(g);
        }
        for(int j = 0; j<n; j++){
            int g;
            cin>>g;
            b.push_back(g);
        }
        vector<int> mi;
        vector<int> ma;
        int right = n-1;
        int ptr1 = right;
        int ptr2 = right;
        while(right>=0){
            while(ptr1>=0 and b[ptr1]>=a[right]){
                // cout<<ptr1<<endl;
                ptr1--;
            }
            // cout<<"hi"<<endl;
            mi.push_back(b[ptr1+1]-a[right]);
            ma.push_back(b[ptr2]-a[right]);
            if(ptr2-ptr1==n-right){
                ptr2 = ptr1;
                n = right;
                //cout<<ptr2<<endl;
            }
            right--;
        }
        reverse(mi.begin(),mi.end());

        for(auto s:mi){
            cout<<s<<" ";
        }
        reverse(ma.begin(),ma.end());
        cout<<endl;
        for(auto s: ma){
            cout<<s<<" ";
        }
        cout<<endl;
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold